#include <iostream>

#define x first
#define y second

using namespace std;

const int N = 1010;

typedef pair<int, int> PII;

int n, m;
PII q[N * N];
char g[N][N];
bool st[N][N];

void bfs(int qx, int qy) {
    int hh = 0, tt = 0;
    q[0] = {qx, qy};
    st[qx][qy] = true;
    
    while (hh <= tt) {
        PII t = q[hh ++ ];
        
        for (int i = t.x - 1; i <= t.x + 1; ++ i)
            for (int j = t.y - 1; j <= t.y + 1; ++ j) {
                if (i < 0 || i >= n || j < 0 || j >= m) continue;
                if (i == t.x && j == t.y) continue;
                if (g[i][j] == '.' || st[i][j]) continue;
                
                q[ ++ tt] = {i, j};
                st[i][j] = true;
            }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    
    for (int i = 0; i < n; ++ i) scanf("%s", g[i]);
    
    int ans = 0;
    for (int i = 0; i < n; ++ i)
        for (int j = 0; j < m; ++ j)
            if (g[i][j] == 'W' && !st[i][j]) {
                bfs(i, j);
                ans ++ ;
            }
    
    printf("%d\n", ans);
    
    return 0;
}